Back
CH
All Podcasts Russian Armed Forces Rockets Experiments Live Outer space Electricity Live television History Electrical Engineering Birds Cats Nature Information Cars Recently uploaded New to you

keyword

Incredible Cat Tech

5.5M views 1 month ago

keyword

Asked
Viewed 70k times
45

I have the following:

for (var i = 0; i < children.length; i++){ if(hasClass(children[i], "lbExclude")){ children[i].parentNode.removeChild(children[i]); } }; 

I would like it to loop through all children's children, etc (not just the top level). I found this line, which seems to do that:

for(var m = n.firstChild; m != null; m = m.nextSibling) { 

But I'm unclear on how I refer to the current child if I make that switch? I would no longer have i to clarify the index position of the child. Any suggestions?

Thanks!

Update:

I'm now using the following, according to answer suggestions. Is this the correct / most efficient way of doing so?

function
 removeTest(child) { if (hasClass(child, "lbExclude")) { child.parentNode.removeChild(child); } } function
 allDescendants(node) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; allDescendants(child); removeTest(child); } } var children = temp.childNodes; for (var i = 0; i < children.length; i++) { allDescendants(children[i]); }; 
CC BYSA 3.0
Mosh Feu
29k1616 gold badges9292 silver badges138138 bronze badges
asked Apr 26, 2010 at 8:53
4

11 Answers 11

59
function
 allDescendants (node) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; allDescendants(child); doSomethingToNode(child); } } 

You loop over all the children, and for each element, you call the same function and have it loop over the children of that element.

CC BYSA 2.5
answered Apr 26, 2010 at 8:56
2
51

Normally you'd have a function that could be called recursively on all nodes. It really depends on what you want to do to the children. If you simply want to gather all descendants, then element.getElementsByTagName may be a better option.

var all = node.getElementsByTagName('*'); for (var i = 1, l = all.length; ++i < l;) { removeTest(all[i]); } 
CC BYSA 2.5
answered Apr 26, 2010 at 9:09
2
4

There's no need for calling the 'allDescendants' method on all children, because the method itself already does that. So remove the last codeblock and I think that is a proper solution (á, not thé =])


 function
 removeTest(child){ if(hasClass(child, "lbExclude")){ child.parentNode.removeChild(child); } } function
 allDescendants (node) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; allDescendants(child); removeTest(child); } } var children = allDescendants(temp); 
CC BYSA 2.5
answered Apr 26, 2010 at 9:28
1
3

You can use BFS to find all the elements.

function(element) { // [].slice.call() HTMLCollection to Array
 var children = [].slice.call(element.children), found = 0; while (children.length > found) { children = children.concat([].slice.call(children[found].children)); found++; } return children; }; 

This function returns all the children's children of the element.

CC BYSA 3.0
answered Feb 2, 2017 at 10:28
0
3

To get all descendants as an array, use this:

function
 getAllDescendants(node) { var all = []; getDescendants(node); function
 getDescendants(node) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; getDescendants(child); all.push(child); } } return all; } 
CC BYSA 4.0
answered Aug 5, 2019 at 14:56
2

The most clearcut way to do it in modern browsers or with babel is this. Say you have an HTML node $node whose children you want to recurse over.

Array.prototype.forEach.call($node.querySelectorAll("*"), function(node) { doSomethingWith(node); }); 

The querySelectorAll('*') on any DOM node would give you all the child nodes of the element in a NodeList. NodeList is an arraylike object, so you can use the Array.prototype.forEach.call to iterate over this list, processing each child onebyone within the callback.

CC BYSA 4.0
answered May 31, 2018 at 11:56
2
1

If you have jquery and you want to get all descendant elements you can use:


 var all_children= $(parent_element).find('*'); 

Just be aware that all_children is an HTML collection and not an array. They behave similarly when you're just looping, but collection doesn't have a lot of the useful Array.prototype methods you might otherwise enjoy.

CC BYSA 3.0
answered Jun 8, 2015 at 20:24
1

if items are being created in a loop you should leave a index via id="" dataname or some thing. You can then index them directly which will be faster for most functions such as (!F). Works pretty well for 1024 bits x 100 items depending on what your doing.

if ( document.getElementById( cid ) ) { return; } else { what you actually want } 

this will be faster in most cases once the items have already been loaded. only scrub the page on reload or secure domain transfers / logins / cors any else and your doing some thing twice.

CC BYSA 4.0
answered Dec 15, 2018 at 13:51
0

If you use a js library it's as simple as this:

$('.lbExclude').remove(); 

Otherwise if you want to acquire all elements under a node you can collect them all natively:

var nodes = node.getElementsByTagName('*'); for (var i = 0; i < nodes.length; i++) { var n = nodes[i]; if (hasClass(n, 'lbExclude')) { node.parentNode.removeChild(node); } } 
CC BYSA 2.5
answered Apr 26, 2010 at 9:35
1
0
TreeNode node = tv.SelectedNode; while (node.Parent != null) { node = node.Parent; } CallRecursive(node); private void
 CallRecursive(TreeNode treeNode) { foreach (TreeNode tn in treeNode.Nodes) { //Write whatever code here this function recursively loops through all nodes 
 CallRecursive(tn); } } 
CC BYSA 4.0
Gust van de Wal
5,29111 gold badge2525 silver badges5151 bronze badges
answered Mar 11, 2020 at 18:29
0

The basic idea is remember that DOM is a tree structure. So, using the recursive approach, the idea is:

  1. Process parent obj;
  2. If obj has children(doesn't matter how many), iterate the children using childNodes function;
  3. Inside iteration, call the same function recursively passing the child object

Ex:

function
 doSomething(obj) { // Processing parent obj here
 if(obj.childNodes !== undefined && obj.childNodes !== null) { obj.childNodes.forEach((c) => { doSomething(c) }) } } 

Note that the 'obj' parameter on doSomething inside the iteration is actually the 'child' object. Thus, you are processing the child node for each interation which means at the end of the loop and all recursive calls, this code will process all nodes in the tree starting from 'obj'.

I hope it helps!

CC BYSA 4.0
answered Apr 1, 2023 at 18:40

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Not the answer you're looking for? Browse other questions tagged or ask your own question.

langjs

Get realtime assistance with your coding queries. Try AI Help now!

Element: getAttributeNames() method

The getAttributeNames() method of the Element interface returns the attribute names of the element as an Array of strings. If the element has no attributes it returns an empty array.

Using getAttributeNames() along with getAttribute(), is a memoryefficient and performant alternative to accessing Element.attributes.

The names returned by getAttributeNames() are qualified attribute names, meaning that attributes with a namespace prefix have their names returned with that namespace prefix (not the actual namespace), followed by a colon, followed by the attribute name (for example, xlink:href), while any attributes which have no namespace prefix have their names returned asis (for example, href).

Syntax

js
getAttributeNames()
 

Parameters

None.

Return value

An (Array) of strings.

Examples

The following example shows how:

  • For an attribute which has a namespace prefix, getAttributeNames() returns that namespace prefix along with the attribute name.
  • For an attribute which has no namespace prefix, getAttributeNames() returns just the attribute name, asis.

It's important to understand that:

  1. An attribute can be present in the DOM with a namespace but lacking a namespace prefix.
  2. For an attribute in the DOM that has a namespace but lacks a namespace prefix, getAttributeNames() will return just the attribute name, with no indication that the attribute is in a namespace.

The example below includes such a "namespaced but without a namespace prefix" case.

js
const element = document.createElement("a");
 // set "href" attribute with no namespace and no namespace prefix element.setAttribute("href",
 "https://example.com");
 // set "href" attribute with namespace and also "xlink" namespace prefix element.setAttributeNS(
 "http://www.w3.org/1999/xlink",
 "xlink:href",
 "https://example.com",
 );
 // set "show" attribute with namespace but no namespace prefix element.setAttributeNS("http://www.w3.org/1999/xlink",
 "show",
 "new");
 // Iterate over element's attributes
 for
 (const name of element.getAttributeNames())
 {
 const value = element.getAttribute(name); console.log(name, value);
 }
 // logs:
 // href https://example.com
 // xlink:href https://example.com
 // show new
 

Specifications

Specification
DOM Standardreffordomelementgetattributenames①

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
getAttributeNames

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Back
CH
CH
Home
Shorts
Subscriptions
You
All Podcasts Russian Armed Forces Rockets Music Machining Live Universe Black cats Electricity Experiments Live television History Electro music Home improvement Information Comedy Recently uploaded Watched New to you
2.1M views 2 years ago
16:45
Now playing
1.5M views 1 year ago
4:03
Now playing
3.3M views 1 year ago
1:49
Now playing
9.2M views 4 years ago
1:08
Now playing
98K views 2 weeks ago
3:31
Now playing
6M views 2 years ago
5:25
Now playing
2.1M views 2 years ago
15:31
Now playing
5.9M views 1 year ago
2:24
Now playing
7.8M views 8 years ago
2:13
Now playing
6.8M views 1 year ago
13:11
Now playing
704K views 1 month ago
1:22
Now playing
249K views 1 month ago
20:02
Now playing
371K views 2 months ago
4:17
Now playing
711K views 1 year ago
1:00
Now playing
2.3M views 1 year ago
3:46
Now playing
2M views 2 years ago
5:15
Now playing
7.4M views 1 year ago
37:31
Now playing
7.2M views 1 year ago
1:33
Now playing
404K views 6 months ago
10:14
Now playing
32K views 2 weeks ago
7:28
Now playing
6M views 1 year ago
3:00
Now playing
380K views 1 year ago
2:51
Now playing
3.1M views 1 year ago
0:55
Now playing

keyword

24M views 10 years ago
10:35
Now playing
21M views 5 years ago
3:33
Now playing
15M views 2 years ago
0:44
Now playing
602K views 3 months ago
1:07
Now playing
135K views 1 year ago
9:15
Now playing
14M views 4 years ago
3:09
Now playing
55M views 7 years ago
7:06
Now playing
11M views 4 years ago
6:54
Now playing
22M views 6 years ago
14:59
Now playing
2.3M views 2 years ago
0:34
Now playing
52M views 9 years ago
1:56
Now playing
93M views 4 years ago
1:35
Now playing
427K views 2 weeks ago
5:06
Now playing
10M views 2 years ago
3:02
Now playing
19M views 3 years ago
2:21
Now playing
2M views 2 years ago
3:59
Now playing
1M views Streamed 3 months ago
1:57
Now playing
20M views 1 year ago
0:42
Now playing
890K views 2 months ago
4:04
Now playing
518K views 3 weeks ago
3:08
Now playing
42M views 4 years ago
2:51
Now playing
2.9M views 11 months ago
8:04
Now playing
60K views 2 weeks ago
0:52
Now playing
24M views 3 years ago
8:05
Now playing
7.9M views 9 months ago
1:37
Now playing
12M views 1 year ago
0:56
Now playing
1.1M views 2 years ago
7:09
Now playing
29M views 1 year ago
1:56
Now playing
16M views 11 months ago
3:14
Now playing
3.1M views 3 years ago
5:52
Now playing
13M views 5 years ago
7:00
Now playing
440K views 3 years ago
3:03
Now playing
6.4M views 10 months ago
0:20
Now playing
23M views 7 years ago
1:45
Now playing
11M views 8 years ago
3:13
Now playing
10M views 2 years ago
2:37
Now playing
863K views 1 year ago
1:47
Now playing
10M views 5 years ago
4:58
Now playing
13M views 5 years ago
8:25
Now playing
9.9M views 7 years ago
30:40
Now playing
781K views 4 months ago
0:31
Now playing
2.9M views 1 year ago
1:49
Now playing
5.4M views 7 years ago
10:06
Now playing
345K views 1 month ago
4:31
Now playing
3.4M views 4 years ago
3:40
Now playing
2.4M views 2 years ago
8:01
Now playing
1.6M views 8 months ago
8:14
Now playing
1M views 3 months ago
0:33
Now playing
3.6M views 2 years ago
8:35
Now playing
1.5M views 2 years ago
9:45
Now playing
4.8M views 3 years ago
3:29
Now playing
78M views 4 years ago
5:04
Now playing
5.5M views 1 year ago
15:11
Now playing
6.8M views 2 years ago
8:14
Now playing
4.1M views 3 years ago
2:25
Now playing
661K views 2 years ago
16:58
Now playing
307K views 1 day ago
2:41
Now playing

keyword

1.6M views 2 months ago
46:58
Now playing
5.8M views 1 year ago
12:29
Now playing
4.2M views 2 years ago
11:56
Now playing
1.1M views 5 months ago
1:52
Now playing
4.3M views 2 years ago
3:03
Now playing
1.1M views 2 years ago
8:16
Now playing
3.4M views 1 month ago
19:53
Now playing
3.6M views 2 years ago
11:03
Now playing
10M views 3 years ago
10:02
Now playing
121M views 7 years ago
1:14:15
Now playing
36M views 10 years ago
2:49
Now playing
2.2M views 4 years ago
Gamepad wireless gaming controller make ike g5 pro elite hall trigger joystick mecha tactile buttons for switch pc android ios
10,000+
Original text
Rate this translation
Your feedback will be used to help improve Google Translate
 
Top priceperformance ratio

We offer competitive prices on more than 100 million items.

 
Shop worldwide

We ship to more than 200 countries and regions and our website is available in more than 12 languages.

 
Secure payment

Pay with the world's most popular and secure payment methods.

 
Shop safely

Our buyer protection policy covers your entire purchase.

 
Help Center

24/7 support for a smooth shopping experience.

 
Better shopping

Download the app and get mobileonly features like image search and discount games.